#include "Player.h"
#include "Bullet.h"
#include "Config.h"

using namespace Ogre;
using namespace NxOgre;

Player::Player() :
	playerID(0),
	playerName(""),
	wpnName(""),
	health(100.00),
	kills(0),
	deaths(0),
	jumpTimer(0.0f),
	playerSceneNode(0),
	playerEntity(0),
	playerChar(0),
	isJumping(false)
{
}

void Player::createPlayer(NxOgre::Scene* nxScene, int id, Vector3 pos)
{	
	playerID = id;
	playerName = "MyPlayerBox" + Ogre::StringConverter::toString(playerID);
	
	// Creates the player
	NxOgre::CharacterParams Params;
	Params.setToDefault();
	Params.mType = NxOgre::CharacterParams::CT_Box;
	Params.mDimensions = NxVec3(14,39,14);
	playerChar = nxScene->createCharacter(playerName, pos, Params);

	// Create mesh
	/*playerChar->attachMesh("P_Torso.mesh");
	playerChar->createNode();
	playerSceneNode = playerChar->getNode();
	Ogre::String temp = playerSceneNode->getName();
	playerEntity = playerChar->getEntity();*/
	
	// Set avatar color
	if (playerID == 1)
	{
		playerChar->attachMesh("P1_Torso.mesh");
		playerChar->createNode();
		playerSceneNode = playerChar->getNode();
		Ogre::String temp = playerSceneNode->getName();
		playerEntity = playerChar->getEntity();
		MaterialPtr mMaterial1 = MaterialPtr(MaterialManager::getSingleton().getByName("p1TorsoCol"));
		mMaterial1->getTechnique(0)->getPass(0)->setDiffuse(Config::P1COLOR);

		// Prepare Anim Settings
		charWalk = playerEntity->getAnimationState("p1Walk");
		charWalk->setEnabled(true);
		charWalk->setLoop(true);
	}
	else
	{
		playerChar->attachMesh("P2_Torso.mesh");
		playerChar->createNode();
		playerSceneNode = playerChar->getNode();
		Ogre::String temp = playerSceneNode->getName();
		playerEntity = playerChar->getEntity();
		MaterialPtr mMaterial2 = MaterialPtr(MaterialManager::getSingleton().getByName("p2TorsoCol"));
		mMaterial2->getTechnique(0)->getPass(0)->setDiffuse(Config::P2COLOR);

		// Prepare Anim Settings
		charWalk = playerEntity->getAnimationState("p2Walk");
		charWalk->setEnabled(true);
		charWalk->setLoop(true);
	}

	// Attach weapon
	Ogre::Bone* wpnBone = playerEntity->getSkeleton()->getBone("b_wpnbone");
		
	wpnName = "wpn"+Ogre::StringConverter::toString(playerID);
	wpnEntity = nxScene->getSceneManager()->createEntity(wpnName,"gun.mesh");
	wpnSceneNode = playerSceneNode->createChildSceneNode(wpnName+"Node",wpnBone->getPosition() + Vector3(0,25,10));
	wpnSceneNode->attachObject(wpnEntity);
}

void Player::reset()
{
	health = 100;
	if (playerID == 1)
		setPosition(P1SHOOTSTARTPOS);
	else if (playerID == 2)
		setPosition(P2SHOOTSTARTPOS);
	jumpTimer = 0.0f;
	isJumping = false;
}

void Player::update(const FrameEvent& evt, bool walkFront, bool walkBack)
{
	if (isJumping)
	{
		// jumpTimer incrementation
		jumpTimer += evt.timeSinceLastFrame;
		if (jumpTimer < 0.3f)
		{
			if (walkBack)
				charWalk->setTimePosition(charWalk->getLength()/4*3);
			else
				charWalk->setTimePosition(charWalk->getLength()/4);
			playerChar->addMovement(Character::DR_Jump_Up);		
		}
		if (jumpTimer > 1.0f)
		{
			jumpTimer = 0.0f;
			charWalk->setTimePosition(0.0f);
			isJumping = false;
		}
	}
	else
	{
		if (walkFront)
		charWalk->addTime(evt.timeSinceLastFrame * 2);
		if (walkBack)
			charWalk->addTime(evt.timeSinceLastFrame * -2);
		if (!walkFront && !walkBack)
			charWalk->setTimePosition(0.0f);
	}	
}

void Player::keyPressed(KeyPress key)
{
	switch (key)
	{
		// Player 1
		case UP:
			playerChar->addMovement(Character::DR_Forward);
			break;
		case DOWN:
			playerChar->addMovement(Character::DR_Backward);
			break;
		case LEFT:
			playerChar->setDirection(playerChar->getGlobalOrientation()*Quaternion(Degree(1.0), Vector3::UNIT_Y)); 
			break;
		case RIGHT:
			playerChar->setDirection(playerChar->getGlobalOrientation()*Quaternion(Degree(-1.0), Vector3::UNIT_Y)); 
			break;
		case JUMP:
			if (jumpTimer == 0.0f)
				isJumping = true;
			break;
		default:
			break;
	}
}